home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / lisp / wgdb-42.lha / wgdb-4.2 / gdb / remote.c < prev    next >
C/C++ Source or Header  |  1992-09-11  |  20KB  |  857 lines

  1. /* Remote target communications for serial-line targets in custom GDB protocol
  2.    Copyright (C) 1988-1991 Free Software Foundation, Inc.
  3.  
  4. This file is part of GDB.
  5.  
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /* Remote communication protocol.
  21.    All values are encoded in ascii hex digits.
  22.  
  23.     Request        Packet
  24.  
  25.     read registers  g
  26.     reply        XX....X        Each byte of register data
  27.                     is described by two hex digits.
  28.                     Registers are in the internal order
  29.                     for GDB, and the bytes in a register
  30.                     are in the same order the machine uses.
  31.             or ENN        for an error.
  32.  
  33.     write regs    GXX..XX        Each byte of register data
  34.                     is described by two hex digits.
  35.     reply        OK        for success
  36.             ENN        for an error
  37.  
  38.     read mem    mAA..AA,LLLL    AA..AA is address, LLLL is length.
  39.     reply        XX..XX        XX..XX is mem contents
  40.             or ENN        NN is errno
  41.  
  42.     write mem    MAA..AA,LLLL:XX..XX
  43.                     AA..AA is address,
  44.                     LLLL is number of bytes,
  45.                     XX..XX is data
  46.     reply        OK        for success
  47.             ENN        for an error
  48.  
  49.     cont        cAA..AA        AA..AA is address to resume
  50.                     If AA..AA is omitted,
  51.                     resume at same address.
  52.  
  53.     step        sAA..AA        AA..AA is address to resume
  54.                     If AA..AA is omitted,
  55.                     resume at same address.
  56.  
  57.     last signal     ?               Reply the current reason for stopping.
  58.                                         This is the same reply as is generated
  59.                     for step or cont : SAA where AA is the
  60.                     signal number.
  61.  
  62.     There is no immediate reply to step or cont.
  63.     The reply comes when the machine stops.
  64.     It is        SAA        AA is the "signal number"
  65.  
  66.     kill req    k
  67. */
  68.  
  69. #include <stdio.h>
  70. #include <string.h>
  71. #include <fcntl.h>
  72. #include "defs.h"
  73. #include "param.h"
  74. #include "frame.h"
  75. #include "inferior.h"
  76. #include "target.h"
  77. #include "wait.h"
  78. #include "terminal.h"
  79.  
  80. #ifdef USG
  81. #include <sys/types.h>
  82. #endif
  83.  
  84. #include <signal.h>
  85.  
  86. extern struct value *call_function_by_hand();
  87. extern void start_remote ();
  88.  
  89. extern struct target_ops remote_ops;    /* Forward decl */
  90.  
  91. static int kiodebug;
  92. static int timeout = 5;
  93.  
  94. #if 0
  95. int icache;
  96. #endif
  97.  
  98. /* Descriptor for I/O to remote machine.  Initialize it to -1 so that
  99.    remote_open knows that we don't have a file open when the program
  100.    starts.  */
  101. int remote_desc = -1;
  102.  
  103. #define    PBUFSIZ    400
  104.  
  105. /* Maximum number of bytes to read/write at once.  The value here
  106.    is chosen to fill up a packet (the headers account for the 32).  */
  107. #define MAXBUFBYTES ((PBUFSIZ-32)/2)
  108.  
  109. static void remote_send ();
  110. static void putpkt ();
  111. static void getpkt ();
  112. #if 0
  113. static void dcache_flush ();
  114. #endif
  115.  
  116.  
  117. /* Called when SIGALRM signal sent due to alarm() timeout.  */
  118. #ifndef HAVE_TERMIO
  119. void
  120. remote_timer ()
  121. {
  122.   if (kiodebug)
  123.     printf ("remote_timer called\n");
  124.  
  125.   alarm (timeout);
  126. }
  127. #endif
  128.  
  129. /* Initialize remote connection */
  130.  
  131. void
  132. remote_start()
  133. {
  134. }
  135.  
  136. /* Clean up connection to a remote debugger.  */
  137.  
  138. /* ARGSUSED */
  139. void
  140. remote_close (quitting)
  141.      int quitting;
  142. {
  143.   if (remote_desc >= 0)
  144.     close (remote_desc);
  145.   remote_desc = -1;
  146. }
  147.  
  148. /* Open a connection to a remote debugger.
  149.    NAME is the filename used for communication.  */
  150.  
  151. void
  152. remote_open (name, from_tty)
  153.      char *name;
  154.      int from_tty;
  155. {
  156.   TERMINAL sg;
  157.  
  158.   if (name == 0)
  159.     error (
  160. "To open a remote debug connection, you need to specify what serial\n\
  161. device is attached to the remote system (e.g. /dev/ttya).");
  162.  
  163.   target_preopen (from_tty);
  164.  
  165.   remote_close (0);
  166.  
  167. #if 0
  168.   dcache_init ();
  169. #endif
  170.  
  171.   remote_desc = open (name, O_RDWR);
  172.   if (remote_desc < 0)
  173.     perror_with_name (name);
  174.  
  175.   ioctl (remote_desc, TIOCGETP, &sg);
  176. #ifdef HAVE_TERMIO
  177.   sg.c_cc[VMIN] = 0;        /* read with timeout.  */
  178.   sg.c_cc[VTIME] = timeout * 10;
  179.   sg.c_lflag &= ~(ICANON | ECHO);
  180. #else
  181.   sg.sg_flags = RAW;
  182. #endif
  183.   ioctl (remote_desc, TIOCSETP, &sg);
  184.  
  185.   if (from_tty)
  186.     printf ("Remote debugging using %s\n", name);
  187.   push_target (&remote_ops);    /* Switch to using remote target now */
  188.  
  189. #ifndef HAVE_TERMIO
  190. #ifndef NO_SIGINTERRUPT
  191.   /* Cause SIGALRM's to make reads fail.  */
  192.   if (siginterrupt (SIGALRM, 1) != 0)
  193.     perror ("remote_open: error in siginterrupt");
  194. #endif
  195.  
  196.   /* Set up read timeout timer.  */
  197.   if ((void (*)()) signal (SIGALRM, remote_timer) == (void (*)()) -1)
  198.     perror ("remote_open: error in signal");
  199. #endif
  200.  
  201.   /* Ack any packet which the remote side has already sent.  */
  202.   write (remote_desc, "+", 1);
  203.   putpkt ("?");            /* initiate a query from remote machine */
  204.  
  205.   start_remote ();        /* Initialize gdb process mechanisms */
  206. }
  207.  
  208. /* remote_detach()
  209.    takes a program previously attached to and detaches it.
  210.    We better not have left any breakpoints
  211.    in the program or it'll die when it hits one.
  212.    Close the open connection to the remote debugger.
  213.    Use this when you want to detach and do something else
  214.    with your gdb.  */
  215.  
  216. static void
  217. remote_detach (args, from_tty)
  218.      char *args;
  219.      int from_tty;
  220. {
  221.   if (args)
  222.     error ("Argument given to \"detach\" when remotely debugging.");
  223.   
  224.   pop_target ();
  225.   if (from_tty)
  226.     printf ("Ending remote debugging.\n");
  227. }
  228.  
  229. /* Convert hex digit A to a number.  */
  230.  
  231. static int
  232. fromhex (a)
  233.      int a;
  234. {
  235.   if (a >= '0' && a <= '9')
  236.     return a - '0';
  237.   else if (a >= 'a' && a <= 'f')
  238.     return a - 'a' + 10;
  239.   else
  240.     error ("Reply contains invalid hex digit");
  241.   return -1;
  242. }
  243.  
  244. /* Convert number NIB to a hex digit.  */
  245.  
  246. static int
  247. tohex (nib)
  248.      int nib;
  249. {
  250.   if (nib < 10)
  251.     return '0'+nib;
  252.   else
  253.     return 'a'+nib-10;
  254. }
  255.  
  256. /* Tell the remote machine to resume.  */
  257.  
  258. void
  259. remote_resume (step, siggnal)
  260.      int step, siggnal;
  261. {
  262.   char buf[PBUFSIZ];
  263.  
  264.   if (siggnal)
  265.     error ("Can't send signals to a remote system.");
  266.  
  267. #if 0
  268.   dcache_flush ();
  269. #endif
  270.  
  271.   strcpy (buf, step ? "s": "c");
  272.  
  273.   putpkt (buf);
  274. }
  275.  
  276. /* Wait until the remote machine stops, then return,
  277.    storing status in STATUS just as `wait' would.
  278.    Returns "pid" (though it's not clear what, if anything, that
  279.    means in the case of this target).  */
  280.  
  281. int
  282. remote_wait (status)
  283.      WAITTYPE *status;
  284. {
  285.   unsigned char buf[PBUFSIZ];
  286.  
  287.   WSETEXIT ((*status), 0);
  288.   getpkt (buf);
  289.   if (buf[0] == 'E')
  290.     error ("Remote failure reply: %s", buf);
  291.   if (buf[0] != 'S')
  292.     error ("Invalid remote reply: %s", buf);
  293.   WSETSTOP ((*status), (((fromhex (buf[1])) << 4) + (fromhex (buf[2]))));
  294.   return 0;
  295. }
  296.  
  297. /* Read the remote registers into the block REGS.  */
  298.  
  299. /* Currently we just read all the registers, so we don't use regno.  */
  300. /* ARGSUSED */
  301. void
  302. remote_fetch_registers (regno)
  303.      int regno;
  304. {
  305.   char buf[PBUFSIZ];
  306.   int i;
  307.   char *p;
  308.   char regs[REGISTER_BYTES];
  309.  
  310.   sprintf (buf, "g");
  311.   remote_send (buf);
  312.  
  313.   /* Reply describes registers byte by byte, each byte encoded as two
  314.      hex characters.  Suck them all up, then supply them to the
  315.      register cacheing/storage mechanism.  */
  316.  
  317.   p = buf;
  318.   for (i = 0; i < REGISTER_BYTES; i++)
  319.     {
  320.       if (p[0] == 0 || p[1] == 0)
  321.     error ("Remote reply is too short: %s", buf);
  322.       regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
  323.       p += 2;
  324.     }
  325.   for (i = 0; i < NUM_REGS; i++)
  326.     supply_register (i, ®s[REGISTER_BYTE(i)]);
  327. }
  328.  
  329. /* Prepare to store registers.  Since we send them all, we have to
  330.    read out the ones we don't want to change first.  */
  331.  
  332. void 
  333. remote_prepare_to_store ()
  334. {
  335.   remote_fetch_registers (-1);
  336. }
  337.  
  338. /* Store the remote registers from the contents of the block REGISTERS. 
  339.    FIXME, eventually just store one register if that's all that is needed.  */
  340.  
  341. /* ARGSUSED */
  342. int
  343. remote_store_registers (regno)
  344.      int regno;
  345. {
  346.   char buf[PBUFSIZ];
  347.   int i;
  348.   char *p;
  349.  
  350.   buf[0] = 'G';
  351.   
  352.   /* Command describes registers byte by byte,
  353.      each byte encoded as two hex characters.  */
  354.  
  355.   p = buf + 1;
  356.   for (i = 0; i < REGISTER_BYTES; i++)
  357.     {
  358.       *p++ = tohex ((registers[i] >> 4) & 0xf);
  359.       *p++ = tohex (registers[i] & 0xf);
  360.     }
  361.   *p = '\0';
  362.  
  363.   remote_send (buf);
  364.   return 0;
  365. }
  366.  
  367. #if 0
  368. /* Read a word from remote address ADDR and return it.
  369.    This goes through the data cache.  */
  370.  
  371. int
  372. remote_fetch_word (addr)
  373.      CORE_ADDR addr;
  374. {
  375.   if (icache)
  376.     {
  377.       extern CORE_ADDR text_start, text_end;
  378.  
  379.       if (addr >= text_start && addr < text_end)
  380.     {
  381.       int buffer;
  382.       xfer_core_file (addr, &buffer, sizeof (int));
  383.       return buffer;
  384.     }
  385.     }
  386.   return dcache_fetch (addr);
  387. }
  388.  
  389. /* Write a word WORD into remote address ADDR.
  390.    This goes through the data cache.  */
  391.  
  392. void
  393. remote_store_word (addr, word)
  394.      CORE_ADDR addr;
  395.      int word;
  396. {
  397.   dcache_poke (addr, word);
  398. }
  399. #endif /* 0 */
  400.  
  401. /* Write memory data directly to the remote machine.
  402.    This does not inform the data cache; the data cache uses this.
  403.    MEMADDR is the address in the remote memory space.
  404.    MYADDR is the address of the buffer in our space.
  405.    LEN is the number of bytes.  */
  406.  
  407. void
  408. remote_write_bytes (memaddr, myaddr, len)
  409.      CORE_ADDR memaddr;
  410.      char *myaddr;
  411.      int len;
  412. {
  413.   char buf[PBUFSIZ];
  414.   int i;
  415.   char *p;
  416.  
  417.   if (len > PBUFSIZ / 2 - 20)
  418.     abort ();
  419.  
  420.   sprintf (buf, "M%x,%x:", memaddr, len);
  421.  
  422.   /* We send target system values byte by byte, in increasing byte addresses,
  423.      each byte encoded as two hex characters.  */
  424.  
  425.   p = buf + strlen (buf);
  426.   for (i = 0; i < len; i++)
  427.     {
  428.       *p++ = tohex ((myaddr[i] >> 4) & 0xf);
  429.       *p++ = tohex (myaddr[i] & 0xf);
  430.     }
  431.   *p = '\0';
  432.  
  433.   remote_send (buf);
  434. }
  435.  
  436. /* Read memory data directly from the remote machine.
  437.    This does not use the data cache; the data cache uses this.
  438.    MEMADDR is the address in the remote memory space.
  439.    MYADDR is the address of the buffer in our space.
  440.    LEN is the number of bytes.  */
  441.  
  442. void
  443. remote_read_bytes (memaddr, myaddr, len)
  444.      CORE_ADDR memaddr;
  445.      char *myaddr;
  446.      int len;
  447. {
  448.   char buf[PBUFSIZ];
  449.   int i;
  450.   char *p;
  451.  
  452.   if (len > PBUFSIZ / 2 - 1)
  453.     abort ();
  454.  
  455.   sprintf (buf, "m%x,%x", memaddr, len);
  456.   remote_send (buf);
  457.  
  458.   /* Reply describes registers byte by byte,
  459.      each byte encoded as two hex characters.  */
  460.  
  461.   p = buf;
  462.   for (i = 0; i < len; i++)
  463.     {
  464.       if (p[0] == 0 || p[1] == 0)
  465.     error ("Remote reply is too short: %s", buf);
  466.       myaddr[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
  467.       p += 2;
  468.     }
  469. }
  470.  
  471. /* Read or write LEN bytes from inferior memory at MEMADDR, transferring
  472.    to or from debugger address MYADDR.  Write to inferior if SHOULD_WRITE is
  473.    nonzero.  Returns length of data written or read; 0 for error.  */
  474.  
  475. /* ARGSUSED */
  476. int
  477. remote_xfer_memory(memaddr, myaddr, len, should_write, target)
  478.      CORE_ADDR memaddr;
  479.      char *myaddr;
  480.      int len;
  481.      int should_write;
  482.      struct target_ops *target;            /* ignored */
  483. {
  484.   int origlen = len;
  485.   int xfersize;
  486.   while (len > 0)
  487.     {
  488.       if (len > MAXBUFBYTES)
  489.     xfersize = MAXBUFBYTES;
  490.       else
  491.     xfersize = len;
  492.  
  493.       if (should_write)
  494.         remote_write_bytes(memaddr, myaddr, xfersize);
  495.       else
  496.     remote_read_bytes (memaddr, myaddr, xfersize);
  497.       memaddr += xfersize;
  498.       myaddr  += xfersize;
  499.       len     -= xfersize;
  500.     }
  501.   return origlen; /* no error possible */
  502. }
  503.  
  504. void
  505. remote_files_info ()
  506. {
  507.   printf ("remote files info missing here.  FIXME.\n");
  508. }
  509.  
  510. /*
  511.  
  512. A debug packet whose contents are <data>
  513. is encapsulated for transmission in the form:
  514.  
  515.     $ <data> # CSUM1 CSUM2
  516.  
  517.     <data> must be ASCII alphanumeric and cannot include characters
  518.     '$' or '#'
  519.  
  520.     CSUM1 and CSUM2 are ascii hex representation of an 8-bit 
  521.     checksum of <data>, the most significant nibble is sent first.
  522.     the hex digits 0-9,a-f are used.
  523.  
  524. Receiver responds with:
  525.  
  526.     +    - if CSUM is correct and ready for next packet
  527.     -    - if CSUM is incorrect
  528.  
  529. */
  530.  
  531. static int
  532. readchar ()
  533. {
  534.   char buf;
  535.  
  536.   buf = '\0';
  537. #ifdef HAVE_TERMIO
  538.   /* termio does the timeout for us.  */
  539.   read (remote_desc, &buf, 1);
  540. #else
  541.   alarm (timeout);
  542.   read (remote_desc, &buf, 1);
  543.   alarm (0);
  544. #endif
  545.  
  546.   return buf & 0x7f;
  547. }
  548.  
  549. /* Send the command in BUF to the remote machine,
  550.    and read the reply into BUF.
  551.    Report an error if we get an error reply.  */
  552.  
  553. static void
  554. remote_send (buf)
  555.      char *buf;
  556. {
  557.  
  558.   putpkt (buf);
  559.   getpkt (buf);
  560.  
  561.   if (buf[0] == 'E')
  562.     error ("Remote failure reply: %s", buf);
  563. }
  564.  
  565. /* Send a packet to the remote machine, with error checking.
  566.    The data of the packet is in BUF.  */
  567.  
  568. static void
  569. putpkt (buf)
  570.      char *buf;
  571. {
  572.   int i;
  573.   unsigned char csum = 0;
  574.   char buf2[500];
  575.   int cnt = strlen (buf);
  576.   char ch;
  577.   char *p;
  578.  
  579.   /* Copy the packet into buffer BUF2, encapsulating it
  580.      and giving it a checksum.  */
  581.  
  582.   p = buf2;
  583.   *p++ = '$';
  584.  
  585.   for (i = 0; i < cnt; i++)
  586.     {
  587.       csum += buf[i];
  588.       *p++ = buf[i];
  589.     }
  590.   *p++ = '#';
  591.   *p++ = tohex ((csum >> 4) & 0xf);
  592.   *p++ = tohex (csum & 0xf);
  593.  
  594.   /* Send it over and over until we get a positive ack.  */
  595.  
  596.   do {
  597.     if (kiodebug)
  598.       {
  599.     *p = '\0';
  600.     printf ("Sending packet: %s (%s)\n", buf2, buf);
  601.       }
  602.     write (remote_desc, buf2, p - buf2);
  603.  
  604.     /* read until either a timeout occurs (\0) or '+' is read */
  605.     do {
  606.       ch = readchar ();
  607.     } while ((ch != '+') && (ch != '\0'));
  608.   } while (ch != '+');
  609. }
  610.  
  611. /* Read a packet from the remote machine, with error checking,
  612.    and store it in BUF.  */
  613.  
  614. static void
  615. getpkt (buf)
  616.      char *buf;
  617. {
  618.   char *bp;
  619.   unsigned char csum;
  620.   int c;
  621.   unsigned char c1, c2;
  622.  
  623. #if 0
  624.   /* Sorry, this will cause all hell to break loose, i.e. we'll end
  625.      up in the command loop with an inferior, but (at least if this
  626.      happens in remote_wait or some such place) without a current_frame,
  627.      having set up prev_* in wait_for_inferior, etc.
  628.  
  629.      If it is necessary to have such an "emergency exit", seems like
  630.      the only plausible thing to do is to say the inferior died, and
  631.      make the user reattach if they want to.  Perhaps with a prompt
  632.      asking for confirmation.  */
  633.  
  634.   /* allow immediate quit while reading from device, it could be hung */
  635.   immediate_quit++;
  636. #endif /* 0 */
  637.  
  638.   while (1)
  639.     {
  640.       /* Force csum to be zero here because of possible error retry.  */
  641.       csum = 0;
  642.       
  643.       while ((c = readchar()) != '$');
  644.  
  645.       bp = buf;
  646.       while (1)
  647.     {
  648.       c = readchar ();
  649.       if (c == '#')
  650.         break;
  651.       *bp++ = c;
  652.       csum += c;
  653.     }
  654.       *bp = 0;
  655.  
  656.       c1 = fromhex (readchar ());
  657.       c2 = fromhex (readchar ());
  658.       if ((csum & 0xff) == (c1 << 4) + c2)
  659.     break;
  660.       printf ("Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
  661.           (c1 << 4) + c2, csum & 0xff, buf);
  662.       write (remote_desc, "-", 1);
  663.     }
  664.  
  665. #if 0
  666.   immediate_quit--;
  667. #endif
  668.  
  669.   write (remote_desc, "+", 1);
  670.  
  671.   if (kiodebug)
  672.     fprintf (stderr,"Packet received :%s\n", buf);
  673. }
  674.  
  675. /* The data cache leads to incorrect results because it doesn't know about
  676.    volatile variables, thus making it impossible to debug functions which
  677.    use hardware registers.  Therefore it is #if 0'd out.  Effect on
  678.    performance is some, for backtraces of functions with a few
  679.    arguments each.  For functions with many arguments, the stack
  680.    frames don't fit in the cache blocks, which makes the cache less
  681.    helpful.  Disabling the cache is a big performance win for fetching
  682.    large structures, because the cache code fetched data in 16-byte
  683.    chunks.  */
  684. #if 0
  685. /* The data cache records all the data read from the remote machine
  686.    since the last time it stopped.
  687.  
  688.    Each cache block holds 16 bytes of data
  689.    starting at a multiple-of-16 address.  */
  690.  
  691. #define DCACHE_SIZE 64        /* Number of cache blocks */
  692.  
  693. struct dcache_block {
  694.     struct dcache_block *next, *last;
  695.     unsigned int addr;    /* Address for which data is recorded.  */
  696.     int data[4];
  697. };
  698.  
  699. struct dcache_block dcache_free, dcache_valid;
  700.  
  701. /* Free all the data cache blocks, thus discarding all cached data.  */ 
  702.  
  703. static void
  704. dcache_flush ()
  705. {
  706.   register struct dcache_block *db;
  707.  
  708.   while ((db = dcache_valid.next) != &dcache_valid)
  709.     {
  710.       remque (db);
  711.       insque (db, &dcache_free);
  712.     }
  713. }
  714.  
  715. /*
  716.  * If addr is present in the dcache, return the address of the block 
  717.  * containing it.
  718.  */
  719.  
  720. struct dcache_block *
  721. dcache_hit (addr)
  722. {
  723.   register struct dcache_block *db;
  724.  
  725.   if (addr & 3)
  726.     abort ();
  727.  
  728.   /* Search all cache blocks for one that is at this address.  */
  729.   db = dcache_valid.next;
  730.   while (db != &dcache_valid)
  731.     {
  732.       if ((addr & 0xfffffff0) == db->addr)
  733.     return db;
  734.       db = db->next;
  735.     }
  736.   return NULL;
  737. }
  738.  
  739. /*  Return the int data at address ADDR in dcache block DC.  */
  740.  
  741. int
  742. dcache_value (db, addr)
  743.      struct dcache_block *db;
  744.      unsigned int addr;
  745. {
  746.   if (addr & 3)
  747.     abort ();
  748.   return (db->data[(addr>>2)&3]);
  749. }
  750.  
  751. /* Get a free cache block, put it on the valid list,
  752.    and return its address.  The caller should store into the block
  753.    the address and data that it describes.  */
  754.  
  755. struct dcache_block *
  756. dcache_alloc ()
  757. {
  758.   register struct dcache_block *db;
  759.  
  760.   if ((db = dcache_free.next) == &dcache_free)
  761.     /* If we can't get one from the free list, take last valid */
  762.     db = dcache_valid.last;
  763.  
  764.   remque (db);
  765.   insque (db, &dcache_valid);
  766.   return (db);
  767. }
  768.  
  769. /* Return the contents of the word at address ADDR in the remote machine,
  770.    using the data cache.  */
  771.  
  772. int
  773. dcache_fetch (addr)
  774.      CORE_ADDR addr;
  775. {
  776.   register struct dcache_block *db;
  777.  
  778.   db = dcache_hit (addr);
  779.   if (db == 0)
  780.     {
  781.       db = dcache_alloc ();
  782.       remote_read_bytes (addr & ~0xf, db->data, 16);
  783.       db->addr = addr & ~0xf;
  784.     }
  785.   return (dcache_value (db, addr));
  786. }
  787.  
  788. /* Write the word at ADDR both in the data cache and in the remote machine.  */
  789.  
  790. dcache_poke (addr, data)
  791.      CORE_ADDR addr;
  792.      int data;
  793. {
  794.   register struct dcache_block *db;
  795.  
  796.   /* First make sure the word is IN the cache.  DB is its cache block.  */
  797.   db = dcache_hit (addr);
  798.   if (db == 0)
  799.     {
  800.       db = dcache_alloc ();
  801.       remote_read_bytes (addr & ~0xf, db->data, 16);
  802.       db->addr = addr & ~0xf;
  803.     }
  804.  
  805.   /* Modify the word in the cache.  */
  806.   db->data[(addr>>2)&3] = data;
  807.  
  808.   /* Send the changed word.  */
  809.   remote_write_bytes (addr, &data, 4);
  810. }
  811.  
  812. /* Initialize the data cache.  */
  813.  
  814. dcache_init ()
  815. {
  816.   register i;
  817.   register struct dcache_block *db;
  818.  
  819.   db = (struct dcache_block *) xmalloc (sizeof (struct dcache_block) * 
  820.                     DCACHE_SIZE);
  821.   dcache_free.next = dcache_free.last = &dcache_free;
  822.   dcache_valid.next = dcache_valid.last = &dcache_valid;
  823.   for (i=0;i<DCACHE_SIZE;i++,db++)
  824.     insque (db, &dcache_free);
  825. }
  826. #endif /* 0 */
  827.  
  828. /* Define the target subroutine names */
  829.  
  830. struct target_ops remote_ops = {
  831.     "remote", "Remote serial target in gdb-specific protocol",
  832.     "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
  833. Specify the serial device it is connected to (e.g. /dev/ttya).",
  834.     remote_open, remote_close,
  835.     0, remote_detach, remote_resume, remote_wait,  /* attach */
  836.     remote_fetch_registers, remote_store_registers,
  837.     remote_prepare_to_store, 0, 0, /* conv_from, conv_to */
  838.     remote_xfer_memory, remote_files_info,
  839.     0, 0, /* insert_breakpoint, remove_breakpoint, */
  840.     0, 0, 0, 0, 0,    /* Terminal crud */
  841.     0, /* kill */
  842.     0,  /* load */
  843.     call_function_by_hand,
  844.     0, /* lookup_symbol */
  845.     0, 0, /* create_inferior FIXME, mourn_inferior FIXME */
  846.     process_stratum, 0, /* next */
  847.     1, 1, 1, 1, 1,    /* all mem, mem, stack, regs, exec */
  848.     0, 0,            /* Section pointers */
  849.     OPS_MAGIC,        /* Always the last thing */
  850. };
  851.  
  852. void
  853. _initialize_remote ()
  854. {
  855.   add_target (&remote_ops);
  856. }
  857.